
package ;

import flixel.FlxSprite;
import flixel.group.FlxGroup;
import flixel.group.FlxTypedGroup;
import flixel.input.mouse.FlxMouse;
import flixel.text.FlxText;
import flixel.tweens.FlxTween;
import flixel.util.FlxColor;
import flixel.FlxObject;
import flixel.util.FlxAngle;
import flixel.FlxG;
import openfl.Assets;

using flixel.util.FlxSpriteUtil;

class Inventory extends FlxGroup
{

	// A background for the inventory
	private var _background:FlxSprite;
	
	// All the items that will be in the inventory
	public var _itemSlots:FlxTypedGroup<InvHolder>;
	
	// The four items that are always displayed
	private var _recentItems:FlxTypedGroup<Item>;
	
	private var _viewUp:Bool;
	//

	public function new()  
    {
		super();
		
		// Create a background for the inventory screen
		_background = new FlxSprite(0, 880);
        _background.makeGraphic(FlxG.width, 920, FlxColor.BLACK);
		_background.drawRect(0, 0, FlxG.width, 2, FlxColor.WHITE);
		add(_background);
		
		// initialize the arrays of items and counts
		_itemSlots = new FlxTypedGroup<InvHolder>();
		
		// Items.xml is an xml file describing all the items in the game
		var itemXml:Xml = Xml.parse(Assets.getText(AssetPaths.Items__xml));
		// We iterate over its elements to load in every inventory item
		var allItems:Iterator<Xml> = itemXml.firstElement().elements();
		var i:Int = 0, j:Int = 0;
		for (itemDesc in allItems)
		{
			var count:Null<Int> = Std.parseInt(itemDesc.get("count"));
			if(count == null)
			{
				count = 0;
			}
			_itemSlots.add(new InvHolder(10 +i * 160, 890 + j * 140, itemDesc.nodeName, itemDesc.get("img"), count));
			i++;

			if (i > 3){
				i = 0;
				j++;
			}
		}
		add(_itemSlots);
		_background.scrollFactor.set();
		_viewUp = false;
    }

    override public function update():Void {
		
    	super.update();
    }
	
	public function getClickedItem():Item
	{
		for (slot in _itemSlots)
		{
			if (slot.overlapsPoint(FlxG.mouse.getScreenPosition()) && (slot._count > 0 || slot._count == -2))
			{
				if (_viewUp)
				{
					switchView();
				}
				return slot.getItem();
			}
		}
		return null;
	}
	
	public function usingMouse():Bool
	{
		return _background.overlapsPoint(FlxG.mouse.getScreenPosition());
	}
	
	public function highlightItem():Void {
		for (slot in _itemSlots) {
			if (slot.overlapsPoint(FlxG.mouse.getScreenPosition())) {
					if (slot._count >= 0 || slot._count == -2) {
						slot.highlightTextOn();
					}
			}
			else {
				slot.highlightTextOff();
			}
		}
	}
	
	public function switchView():Void
	{
		if (Lambda.fold(_itemSlots.members, function(I:InvHolder, acc:Bool) { return I.overlapsPoint(FlxG.mouse.getScreenPosition()) || acc; }, false) && !_viewUp)
		{
			return;
		}
		if (_background.y < 880 && _background.y > 80)
		{
			return;
		}
		_viewUp = !_viewUp;
		FlxTween.tween(_background, { y: ((_background.y == 880) ? 80 : 880) }, .5);
		_itemSlots.forEach(function (I:InvHolder) { I.forEach( function (spr:FlxSprite) { FlxTween.tween(spr, { y: ((_background.y == 880) ? spr.y - 800 : spr.y + 800) }, .5); } ); } );
	}
	
	public function addItem(itemName:String):Void
	{
		for (slot in _itemSlots)
		{
			if (slot._itemName == itemName)
			{
				slot.incrementCount();
			}
		}
	}

}